package com.pig4cloud.pig.yin.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.security.annotation.Inner;
import com.pig4cloud.pig.yin.entity.WzContactUnitEntity;
import com.pig4cloud.pig.yin.service.WzContactUnitService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.pig4cloud.plugin.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 联络信息表
 *
 * @author pig
 * @date 2025-07-29 11:02:46
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/wzContactUnit" )
@Tag(description = "wzContactUnit" , name = "联络信息表管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class WzContactUnitController {

    private final  WzContactUnitService wzContactUnitService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param wzContactUnit 联络信息表
     * @return
     */

    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('pig_wzContactUnit_view')" )
    public R getWzContactUnitPage(@ParameterObject Page page, @ParameterObject WzContactUnitEntity wzContactUnit) {
        return R.ok(wzContactUnitService.selectWzContactUnitList(page,wzContactUnit));
    }


    /**
     * 通过id查询联络信息表
     * @param unitId id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{unitId}" )
    @PreAuthorize("@pms.hasPermission('pig_wzContactUnit_view')" )
    public R getById(@PathVariable("unitId" ) Long unitId) {
        return R.ok(wzContactUnitService.getById(unitId));
    }

    /**
     * 新增联络信息表
     * @param wzContactUnit 联络信息表
     * @return R
     */
    @Operation(summary = "新增联络信息表" , description = "新增联络信息表" )
    @SysLog("新增联络信息表" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('pig_wzContactUnit_add')" )
    public R save(@RequestBody WzContactUnitEntity wzContactUnit) {
        return R.ok(wzContactUnitService.save(wzContactUnit));
    }

    /**
     * 修改联络信息表
     * @param wzContactUnit 联络信息表
     * @return R
     */
    @Operation(summary = "修改联络信息表" , description = "修改联络信息表" )
    @SysLog("修改联络信息表" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('pig_wzContactUnit_edit')" )
    public R updateById(@RequestBody WzContactUnitEntity wzContactUnit) {
        return R.ok(wzContactUnitService.updateById(wzContactUnit));
    }

    /**
     * 通过id删除联络信息表
     * @param ids unitId列表
     * @return R
     */
    @Operation(summary = "通过id删除联络信息表" , description = "通过id删除联络信息表" )
    @SysLog("通过id删除联络信息表" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('pig_wzContactUnit_del')" )
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(wzContactUnitService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param wzContactUnit 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('pig_wzContactUnit_export')" )
    public List<WzContactUnitEntity> export(WzContactUnitEntity wzContactUnit,Long[] ids) {
        return wzContactUnitService.list(Wrappers.lambdaQuery(wzContactUnit).in(ArrayUtil.isNotEmpty(ids), WzContactUnitEntity::getUnitId, ids));
    }
}